﻿using System;
using System.IO;

namespace ServerRPG
{
    static class LogManager
    {
        private static string _filepath = "";
        public static string FilePath { set { _filepath = value; } get { return _filepath; } }
        private static string _filename = "";
        public static string FileName { set { _filename = value; } get { return _filename; } }

        // Write string to file. 'true' if success.
        public static bool Write(string msg)
        {
            if (_filepath == String.Empty || _filename == String.Empty || msg == String.Empty)
            {
                Console.WriteLine("Ошибка записи в журнал событий. Строка записи, имя или путь к файлу не могут быть пустой строкой.");
                return false;
            }

            // Create directory.
            if (!Directory.Exists(_filepath))
            {
                try
                {
                    Directory.CreateDirectory(_filepath);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }

            // Write to file.
            try
            {
                using (StreamWriter sw = new StreamWriter(_filepath + _filename, true))
                {
                    sw.WriteLine(DateTime.Now.ToString() + " " + msg);
                    sw.Close();

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }

            return true;
        }

        // Write to console too.
        public static bool WriteExt(string msg)
        {
            if (!Write(msg))
                return false;
            Console.WriteLine("> " + msg);

            return true;
        }
    }
}